var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getUserList',(req,res) =>{
    var params = req.query || req.params
    let start = (params.page - 1) * 10;
    var sql = $sql.user.count;
    let allCount = 0;
    conn.query(sql,function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.user.get;
            conn.query(sql,[start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var userList = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:userList
                    }))
                }
            })
        }
    })
})

router.get('/searchUserList',(req,res) =>{
    var params = req.query || req.params
    let start = (params.page - 1) * 10;
    var sql = $sql.user.searchCount;
    let user_type = parseInt(params.user_type)
    let allCount = 0;
    conn.query(sql,[user_type],function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.user.searchList;
            conn.query(sql,[user_type,start],function(err,searchResult){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var userList = searchResult;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:userList
                    }))
                }
            })
        }
    })
})

router.post('/addUser',(req,res)=>{
    var sql = $sql.user.add
    var params = req.body
    params.create_time = new Date();
    params.modify_time = new Date();

    params.user_type = parseInt(params.user_type);
    console.log(params)
    conn.query(sql, [params], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/editUser',(req,res)=>{
    var sql = $sql.user.update
    var params = req.body;
    params.user_id = parseInt(params.user_id);
    params.user_type = parseInt(params.user_type);
    params.is_del = 0;
    params.create_time = new Date(params.create_time);
    params.modify_time = new Date();
    console.log(params)
    conn.query(sql, [params,params.user_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/deleteUser',(req,res)=>{
    var sql = $sql.user.update
    var params = req.body
    params.is_del = 1;
    params.modify_time = new Date();
    console.log(params)
    conn.query(sql, [params,params.user_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/login',(req,res) =>{
    var sql = $sql.user.login
    var params = req.body
    var list=[];
    let success = false;

    console.log(params)
    conn.query(sql, [], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            list = result
            for(let admin of list){
                if(admin.user_mail==params.user_mail&&admin.user_password==params.user_password){
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        user:admin
                    }))
                    return;
                }else if(admin.user_mail!=params.user_mail&&admin.user_password==params.user_password||
                    admin.user_mail==params.user_mail&&admin.user_password!=params.user_password){
                        res.send(JSON.stringify({
                            msg:"账号或密码错误",
                            status:402,
                        }))
                        return;
                }
            }
            res.send(JSON.stringify({
                msg:"用户不存在",
                status:402,
            }))
        }
    })
})

router.get('/getFlightList',(req,res) =>{
    var params = req.query || req.params
    var sql = $sql.user.getFlight;
    // let from_city = decodeURI(params.from_city);
    // let to_city = decodeURI(params.to_city);
    let type = parseInt(params.class_type);
    let allCount = 0;
    console.log("code",params.from_city,params.to_city);
    conn.query(sql,[],function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result);
            // let list=[];
            // let allCount = 0;
            // for(let i=0;i<result.length;i++){
            //     //console.log("res",result[i]);
            //     if(result[i].to_city == new String(params.to_city)&&
            //        result[i].to_city == new String(params.to_city)){
            //         list.push(result[i]);
            //     }
            // }
            //console.log("list",list);
            res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:result
                    }))
        }
    })
})

router.post('/changePwd',(req,res)=>{
    var sql = $sql.user.update
    var params = req.body;
    params.user_id = parseInt(params.user_id);
    params.modify_time = new Date();
    console.log(params)
    conn.query(sql, [params,params.user_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

module.exports = router;